<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-details-buttons xs:flex xs:flex-col">
  <div class="flex flex-row xs:flex-col" style="padding-left: 8px">
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'open')"
      [class.!hidden]="!(!isEdit && !isDetailsPage)">
      {{'common.open-details-page' | translate }}
    </button>
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'checkConnectivity')"
      [class.!hidden]="!showConnectivityDialog()">
      <mat-icon>check_circle_outline</mat-icon>
      <span translate>mqtt-client-credentials.connectivity.check-connectivity</span>
    </button>
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'changePassword')"
      [class.!hidden]="!(!hideChangePassword() && !isEdit)">
      <mat-icon>lock</mat-icon>
      <span translate>profile.change-password</span>
    </button>
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'delete')"
      [class.!hidden]="!(!hideDelete() && !isEdit)">
      <mat-icon svgIcon="mdi:trash-can-outline"></mat-icon>
      <span translate>mqtt-client-credentials.delete-client-credentials</span>
    </button>
  </div>
  <div class="flex flex-row xs:flex-col" style="padding-left: 8px">
    <tb-copy-content-button [cbContent]="entity?.id"
    [isEdit]="isEdit"></tb-copy-content-button>
    <tb-copy-content-button [title]="'mqtt-client-credentials.copy-client-credentials-id'"
      [cbContent]="entity?.credentialsId"
    [isEdit]="isEdit"></tb-copy-content-button>
  </div>
</div>
<div class="mat-padding form-container flex flex-col">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit">
      <mat-form-field class="flex flex-1">
        <mat-label translate>mqtt-client-credentials.name</mat-label>
        <input matInput formControlName="name" required>
        @if (entityForm.get('name').hasError('required')) {
          <mat-error>
            {{ 'mqtt-client-credentials.name-required' | translate }}
          </mat-error>
        }
      </mat-form-field>
      <mat-form-field class="flex flex-1" subscriptSizing="dynamic">
        <mat-label translate>mqtt-client.client-type</mat-label>
        <mat-select required formControlName="clientType">
          @for (clientType of clientTypes; track clientType) {
            <mat-option [value]="clientType">
              {{ clientTypeTranslationMap.get(clientType) | translate }}
            </mat-option>
          }
        </mat-select>
        @if (entityForm.get('clientType').hasError('required')) {
          <mat-error>
            {{ 'mqtt-client.client-type-required' | translate }}
          </mat-error>
        }
      </mat-form-field>
      @if (entityForm.get('clientType').value === ClientType.APPLICATION) {
        <div class="tb-hint"
          [innerHtml]="('mqtt-client-credentials.hint-client-type-application' | translate) + ' ' + ('mqtt-client-credentials.hint-client-type-read-more' | translate)">
        </div>
      }
      @if (entityForm.get('clientType').value === ClientType.DEVICE) {
        <div class="tb-hint"
          [innerHtml]="('mqtt-client-credentials.hint-client-type-device' | translate) + ' ' + ('mqtt-client-credentials.hint-client-type-read-more' | translate)">
        </div>
      }
      <mat-form-field class="mat-block">
        <mat-label translate>mqtt-client-credentials.type</mat-label>
        <mat-select formControlName="credentialsType">
          @for (credentialsType of credentialsTypes; track credentialsType) {
            <mat-option [value]="credentialsType">
              {{ credentialsTypeTranslationMap.get(credentialsType) | translate }}
            </mat-option>
          }
        </mat-select>
        @if (entityForm.get('credentialsType').hasError('required')) {
          <mat-error>
            {{ 'mqtt-client-credentials.type-required' | translate }}
          </mat-error>
        }
      </mat-form-field>
      <div>
        @switch (entityForm.get('credentialsType').value) {
          @case (credentialsType.MQTT_BASIC) {
            <tb-mqtt-credentials-basic formControlName="credentialsValue"
              [entity]="entity">
            </tb-mqtt-credentials-basic>
          }
          @case (credentialsType.SSL) {
            <tb-mqtt-credentials-ssl formControlName="credentialsValue" [entity]="entity"></tb-mqtt-credentials-ssl>
          }
          @case (credentialsType.SCRAM) {
            <tb-mqtt-credentials-scram formControlName="credentialsValue" [entity]="entity"></tb-mqtt-credentials-scram>
          }
        }
      </div>
    </fieldset>
  </form>
</div>
